package com.lottery.ruok.lottrey.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.AllLotteryListViewAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetWholeCategoryRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.lottery.ruok.lottrey.utils.JsonUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllLotteryFragment extends BaseFragment {
    @BindView(R.id.listview)
    ListView listView;

    ArrayList<GetWholeCategoryResponse> datas;

    public static AllLotteryFragment newInstance(){
        return new AllLotteryFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_all_lottery, null);
        ButterKnife.bind(this, view);
        return view;
    }

    public void refresh(ArrayList<GetWholeCategoryResponse> getWholeCategoryResponses){
        Gson gson = new Gson();
        String jsonTran = gson.toJson(getWholeCategoryResponses);
        datas = JsonUtils.jsonToArrayList(jsonTran, GetWholeCategoryResponse.class);
        datas = getWholeCategoryResponses;
        for(GetWholeCategoryResponse getWholeCategoryResponse : datas){
            if(getWholeCategoryResponse.getName().equals("全部")){
                datas.remove(getWholeCategoryResponse);
                break;
            }
        }
        if(getContext() != null){
            AllLotteryListViewAdapter listViewAdapter = new AllLotteryListViewAdapter(datas, getContext(), AllLotteryFragment.this);
            listView.setAdapter(listViewAdapter);

            listView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    setSwipState();
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                }
            });
        }
    }



    public void setSwipState(){
        if (listView != null && listView.getChildAt(0) != null) {
            ((BuyLotteryMainFragment)getParentFragment()).swipeRefreshLayout.setEnabled(listView.getFirstVisiblePosition() == 0 && listView.getChildAt(0).getTop() == 0);
        }
    }
}